package com.yr.ryjk.hospital.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yr.ryjk.common.Pagination;
import com.yr.ryjk.hospital.dao.HospitalMapper;
import com.yr.ryjk.hospital.entity.Hospital;
import com.yr.ryjk.hospital.vo.HospitalVO;
import com.yr.ryjk.util.IDGenerator;

@Service
public class HospitalService implements IHospitalService{
	@Autowired
	private HospitalMapper hospitalMapper;
	
	/**
	 * 处理医生注册时提交的医院(如果hospital表中已存在,则返回id;如果不存在,则新增并)
	 * @param hospitalName
	 * @return
	 */
	@Override
	public String addHandleRegisterHospital(String hospitalName){
		Hospital hospital=hospitalMapper.selectByName(hospitalName);
		if(hospital==null){
			hospital=new Hospital();
			hospital.setId(IDGenerator.gen32UUID());
			hospital.setName(hospitalName);
			hospitalMapper.insert(hospital);
		}
		return hospital.getId();
	}
	
	@Override
	public Object getListOfPage(int pageNo, int pageSize,String province,String city,String county) {
		List<HospitalVO> list=hospitalMapper.selectOfPage((pageNo-1)*pageSize,pageSize,province,city,county);
		int count=hospitalMapper.selectCount(province,city,county);
		
		Map<String, Object> map=new HashMap<String,Object>();
		map.put("list", list);
		map.put("pagination", new Pagination(pageSize, pageNo, count));
		return map;
	}

	@Override
	public Object getInfoById(String id) {
		return hospitalMapper.selectById(id);
	}
	
	@Override
	public void update(String id, String name, String level, String location,
			String county) {
		hospitalMapper.updateById(id,name,level,location,county);
	}

	@Override
	public List<Hospital> getLikelyList(String name) {
		return hospitalMapper.selectLikely(name);
	}

	@Override
	public void insert(String name, String level, String location, int did) {
			Hospital hos = new Hospital();
			
			hos.setId(IDGenerator.gen32UUID());
			hos.setName(name);
			hos.setLevel(level);
			hos.setLocation(location);
			hos.setdId(did);
			
			hospitalMapper.insert(hos);
		
	}

	@Override
	public List<Hospital> getAllHospital() 
	{
		return hospitalMapper.selectAllHospital();
	}
}